let peopleMessage=document.getElementsByClassName("peopleMessage")[0];
let headerPictureReadH3=document.getElementsByClassName("headerPictureReadH3")[0];
document.addEventListener('scroll',function(){
    let topJuLi = document.documentElement.scrollTop;
    peopleMessage.style.top = topJuLi+"px";
},true);
/*标签*/
let BlogType=document.getElementsByClassName("BlogType")[0];
let articleTitle=document.getElementsByClassName("articleTitle")[0];

if(BlogType.innerHTML[0]==="生"){
    articleTitle.innerHTML+='<span>生活</span>'
}
if(BlogType.innerHTML[2]==="二"){
    articleTitle.innerHTML+='<span>二次元</span>'
}
if(BlogType.innerHTML[4]==="音"){
    articleTitle.innerHTML+='<span>音乐</span>'
}
if(BlogType.innerHTML[6]==="美"){
    articleTitle.innerHTML+='<span>美食</span>'
}
if(BlogType.innerHTML[8]==="影"){
    articleTitle.innerHTML+='<span>影视</span>'
}
if(BlogType.innerHTML[10]==="运"){
    articleTitle.innerHTML+='<span>运动</span>'
}
if(BlogType.innerHTML[12]==="科"){
    articleTitle.innerHTML+='<span>科技</span>'
}
if(BlogType.innerHTML[14]==="技"){
    articleTitle.innerHTML+='<span>技术</span>'
}
if(BlogType.innerHTML[16]==="社"){
    articleTitle.innerHTML+='<span>社交</span>'
}
if(BlogType.innerHTML[18]==="其"){
    articleTitle.innerHTML+='<span>其他</span>'
}
/*点赞*/
let lickCollectionComment=document.getElementsByClassName("lickCollectionComment")[0];
let readContain=document.getElementsByClassName("readContain")[0];
let addArticle=document.getElementsByClassName("addArticle")[0];
Top();
window.onresize = function(){
    Top();
}
document.onscroll=function(){
    Top();
}
function Top(){
    if(addArticle.offsetHeight>document.documentElement.scrollTop+document.documentElement.clientHeight-350){
        lickCollectionComment.style.top=(document.documentElement.scrollTop+document.documentElement.clientHeight-125)+"px";
    }else{
        lickCollectionComment.style.top="";
    }
}
let lickB=document.getElementsByClassName("lickB");
let commentInputContent=document.getElementsByClassName("commentInputContent")[0];
lickB[2].onclick=function(){
    returnPositionX(commentInputContent.offsetTop-document.documentElement.clientHeight+120);
}
function returnPositionX(e) {
    let backTopTimer = setInterval(function() {
        let topH = document.documentElement.scrollTop || document.body.scrollTop;
        if(topH < e) {
            document.documentElement.scrollTop = document.body.scrollTop = topH +30;
        }else{
            clearInterval(backTopTimer);
        }
    }, 10);
}
let commentBtn=document.getElementsByClassName("commentBtn")[0]
let commentInput=document.getElementsByClassName("commentInput")[0];
commentInput.onfocus=function(){
    commentInputContent.style.height='70px'
    commentInput.style.boxShadow="0 0 6px 0 #5094d5";
}
commentInput.onblur=function(){
    commentInputContent.style.height='35px'
    commentInput.style.boxShadow="";
}

let BlogId=document.getElementsByClassName("BlogId")[0];
let lick=document.getElementsByClassName("lick");
let lickContent=document.getElementsByClassName("lickContent")[0];
/*关注*/
let BlogUserId=document.getElementsByClassName("BlogUserId")[0];
let follow=document.getElementsByClassName("follow")[0];
let fanNumber=document.getElementsByClassName("fanNumber")[0];
/*收藏*/
let collectionContent=document.getElementsByClassName("collectionContent")[0];
let collection=document.getElementsByClassName("collection")[0];
if(sessionStorage.getItem("id")==null){

    /*如果没有登录将点赞收藏关注设置成默认样式*/
    lickContent.style.backgroundImage="url(../static_resources/img/D.png)";
    collectionContent.style.backgroundImage="url(../static_resources/img/S.png)";
    follow.innerHTML="关注";
    follow.style.border=""
    follow.style.color="";
    /*如果没有登录用thymeleaf遍历数据*/

    /*如果没有登录点击出现弹窗提示登录*/
    lickB[0].onclick=function(){
        Tips("你还未登录不能点赞")
    }
    lickB[1].onclick=function(){
        Tips("你还未登录不能收藏")
    }
    follow.onclick=function(){
        Tips("你还未登录不能关注");
    }
    commentInput.oninput=function(){
        if(commentInput.value===""){
            commentBtn.style.backgroundColor="#ccccd7";
        }else{
            commentBtn.style.backgroundColor="#42bbfd";
            commentBtn.onclick=function(){
                Tips("你还未登录不能发布评论");
            }
        }
    }
}else{
    lickB[0].onclick=function(){
        /*点赞*/
        D();
    }

    /*点赞*/
    function D(){
        $.ajax({
            url:"../thumbsUp/ThumbsUp",
            data:{
                blogid:BlogId.innerHTML,
                userid:sessionStorage.getItem("id")
            },
            success:function(data){
                TFD();
            }
        });
    }
    /*判断是否点赞*/
    TFD();
    function TFD(){
        $.ajax({
            url:"../judge",
            data:{
                blogid:BlogId.innerHTML,
                userid:sessionStorage.getItem("id")
            },
            success:function(data){
                lick[0].innerHTML=data.data;
                if(data.msg==0){
                    lickContent.style.backgroundImage="url(../static_resources/img/D.png)";
                }else{
                    lickContent.style.backgroundImage="url(../static_resources/img/DD.png)";
                }
            }
        });
    }


    lickB[1].onclick=function(){
        /*收藏*/
        S();
    }
    /*收藏*/
    function S(){
        $.ajax({
            url:"../collection/Collection",
            data:{
                blogid:BlogId.innerHTML,
                userid:sessionStorage.getItem("id")
            },
            success:function(data){
                TFS();
            }
        });
    }
    /*判断是否收藏*/
    TFS();
    function TFS(){
        $.ajax({
            url:"../judgeCollection",
            data:{
                blogid:BlogId.innerHTML,
                userid:sessionStorage.getItem("id")
            },
            success:function(data){
                collection.innerHTML=data.data;
                if(data.msg==0){
                    collectionContent.style.backgroundImage="url(../static_resources/img/S.png)";
                }else{
                    collectionContent.style.backgroundImage="url(../static_resources/img/SS.png)";
                }
            }
        });
    }
    /*点击关注*/
    follow.onclick=function(){
        $.ajax({
            url:'../follow/Follow',
            data:{
                bloggerid:BlogUserId.innerHTML,
                userid:sessionStorage.getItem("id")
            },
            success:function(data){
                TFG();
            }
        });
    }
    /*判断是否关注*/
    TFG();
    function TFG(){
        $.ajax({
            url:'../judgeFollow',
            data:{
                userid:sessionStorage.getItem("id"),
                bloggerid:BlogUserId.innerHTML
            },
            success:function(data){
                if(data.msg==0){
                    fanNumber.innerHTML=data.data;
                    follow.innerHTML="关注";
                    follow.style.border=""
                    follow.style.color="";
                }else{
                    fanNumber.innerHTML=data.data;
                    follow.innerHTML="已关注";
                    follow.style.border="1px solid #42bbfd"
                    follow.style.color="#42bbfd";
                }
            }
        });
    }
    let commentEach=document.getElementsByClassName("commentEach")[0];
    // let commentContentS=document.getElementsByClassName("commentContentS")[0];
    let strContent =null;
    commentBtn.style.backgroundColor="#ccccd7";
    commentInput.oninput=function(){
        onFocus();
        function onFocus(){
            if(commentInput.value==""){
                commentBtn.style.backgroundColor="#ccccd7";
                commentBtn.onclick=function(){

                }
            }else{
                commentBtn.style.backgroundColor="#42bbfd";
                commentBtn.onclick=function(){
                    strContent =commentInput.value
                    // strContent = strContent.replace(/\r\n/g, '<br/>'); //IE9、FF、chrome
                    // strContent = strContent.replace(/\n/g, '<br/>'); //IE7-8
                    // strContent = strContent.replace(/\s/g, '&nbsp;'); //空格处理
                    /*点击添加评论*/
                    addComment();
                    /*遍历评论*/
                    findComment();
                    // DELETE();
                    commentInput.value="";
                    /*发布成功后再次调用判断*/
                    onFocus();
                }
            }
        }
    }


    let titleTwo=document.getElementsByClassName("titleTwo")[0]


    /*主评论*/
    /*遍历评论*/
    findComment();
    function findComment(){
        $.ajax({
            url:'../comment/findComment',
            type:'post',
            data:{
                /*博客博主名字*/
                // blogger:headerPictureReadH3.innerHTML,
                id:BlogUserId.innerHTML,
                /*博客id*/
                blogid:BlogId.innerHTML
            },
            success:function(data){
                let child=commentEach.lastElementChild;
                while(child){
                    commentEach.removeChild(child);
                    child=commentEach.lastElementChild;
                }
                if(data.data!=null) {
                    for (let i = 0; i < data.data.length; i++) {
                        if (sessionStorage.getItem("id") == data.data[i].userid) {
                            // console.log(data);
                            commentEach.innerHTML += '<div class="commentContentS commentContentSS">' +
                                '<span class="AAA" style="display:none">'+i+'</span>'+
                                '<div class="CommentSon">回复</div>' +
                                '<div class="CommentDelete">删除</div>' +
                                '' + '<span class="commentUserName">' +
                                '<span class="commentUserNickName">' +
                                '' + data.data[i].blogger +
                                '</span>' + '：' +
                                '</span>' +
                                data.data[i].content +
                                '<div class="commentInputCryC">' +
                                '<textarea class="commentInputCry" value="发表评论"></textarea>' +
                                '<div class="CommentTrue">确定发布</div>' +
                                '<div class="CommentFalse">取消发布</div>' +
                                '</div>' +
                                '<div class="SonCommentContent"></div>' +
                                '</div>'
                        } else {
                            commentEach.innerHTML += '<div class="commentContentS commentContentSS">' +
                                '<span class="AAA" style="display:none">'+i+'</span>'+
                                '<div class="CommentSon">回复</div>' +
                                '' + '<span class="commentUserName">' +
                                '<span class="commentUserNickName">' +
                                '' + data.data[i].blogger +
                                '</span>' + '：' +
                                '</span>' +
                                data.data[i].content +
                                '<div class="commentInputCryC">' +
                                '<textarea class="commentInputCry" value="发表评论"></textarea>' +
                                '<div class="CommentTrue">确定发布</div>' +
                                '<div class="CommentFalse">取消发布</div>' +
                                '</div>' +
                                '<div class="SonCommentContent"></div>' +
                                '</div>'
                        }
                    }
                }
                /*绑定属性*/
                DELETE();
            }
        });
    }
    /*发表评论*/
    function addComment(){
        $.ajax({
            url:'../comment/addComment',
            type:'post',
            data:{
                /*评论用户昵称*/
                blogger:sessionStorage.getItem("nickname"),
                /*被评论的文章id*/
                blogid:BlogId.innerHTML,
                /*被评论的文章标题*/
                blogname:titleTwo.innerHTML,
                /*评论内容*/
                content:strContent,
                /*评论用户id*/
                userid:sessionStorage.getItem("id")
            },
            success:function(data){
                // console.log(data);
            }
        });

    }
}

/*给div添加属性用来遍历子评论和删除评论*/
DELETE();
function DELETE(){
    let commentContentS=document.getElementsByClassName("commentContentS");
    let CommentDelete=document.getElementsByClassName("CommentDelete");
    let CommentSon=document.getElementsByClassName("CommentSon");
    let commentInputCry=document.getElementsByClassName("commentInputCry");
    let commentInputCryC=document.getElementsByClassName("commentInputCryC");
    let CommentTrue=document.getElementsByClassName("CommentTrue");
    let CommentFalse=document.getElementsByClassName("CommentFalse");
    let SonCommentContent=document.getElementsByClassName("SonCommentContent");
    $.ajax({
        url:'../comment/findComment',
        type:'post',
        data:{
            /*博客博主名字*/
            // blogger:headerPictureReadH3.innerHTML,
            id:BlogUserId.innerHTML,
            /*博客id*/
            blogid:BlogId.innerHTML
        },
        success:function(data){
            for(let i=0;i<commentContentS.length;i++){
                commentContentS[i].commentId=data.data[i].commentid;
                commentContentS[i].userId=data.data[i].userid;
            }
            for(let i=0;i<commentContentS.length;i++){
                CommentSon[i].onclick=function(){
                    // console.log(i);
                    commentInputCryC[i].style.display="block";

                }
                /*取消发布*/
                CommentFalse[i].onclick=function(){
                    commentInputCryC[i].style.display="none";
                    /*将输入框中的东西删除*/
                    commentInputCry[i].value="";
                }
                /*确定发布*/
                commentInputCry[i].oninput=function(){
                    if(commentInputCry[i].value==""){
                        CommentTrue[i].style.backgroundColor="#ccccd7"
                        CommentTrue[i].onclick=function(){
                            // console.log("发布失败")
                        }
                    }else{
                        CommentTrue[i].style.backgroundColor="#42bbfd"
                        CommentTrue[i].onclick=function(){
                            /*这里写增加子评论*/
                            SONCOMMENT(commentContentS[i].userId,BlogId.innerHTML,commentContentS[i].commentId,commentInputCry[i].value,sessionStorage.getItem("id"),commentContentS[i].commentId,SonCommentContent[i]);
                            commentInputCryC[i].style.display="none";
                            commentInputCry[i].value="";
                            // FindCommentSon(commentContentS[i].commentId,SonCommentContent[i]);
                        }
                    }
                }
                // CommentSon[i].onfocus=function(){
                //     console.log(i);
                //     commentInputCryC[i].display="none";
                // }
                /*评论id*/
                //commentContentS[i].commentId=data.data[i].commentid
                /*评论用户id*/
                // commentContentS[i].userId=data.data[i].userid
                /*评论者id*/
                // sessionStorage.getItem("id")
                /*当前博客id*/
                // BlogId.innerHTML
            }
            for(let i=0;i<commentContentS.length;i++){
                // console.log("1")
                FindCommentSon(commentContentS[i].commentId,SonCommentContent[i]);
            }
            /*评论删除*/
            for(let i=0;i<CommentDelete.length;i++){
                CommentDelete[i].onclick=function(){
                    // console.log(CommentDelete[i].parentNode.commentId);
                    let r=confirm("你确定要删除这条评论吗");
                    if (r===true) {
                        // console.log(CommentDelete[i].parentNode.commentId)
                        DeleteComment(CommentDelete[i].parentNode.commentId);
                    }
                    else {

                    }
                }
            }
        }
    });
}
function DeleteComment(i){
    $.ajax({
        url:'../comment/deleteComment',
        data:{
            commentid:i
        },
        success:function(data){
            findComment();
        }
    });
}
let tips=document.getElementsByClassName("tips")[0];
function Tips(i) {
    tips.innerHTML = i;
    tips.style.top = "56px";
    setTimeout(function () {
        tips.style.top = "-56px";
    }, 1000)
}

function SONCOMMENT(bloggeridS,blogidS,commentidS,contentS,useridS,i,T){
    $.ajax({
        type:'post',
        url:'../insertCommentSon',
        data:{
            bloggerid:bloggeridS,
            blogid:blogidS,
            commentid:commentidS,
            content:contentS,
            userid:useridS
        },
        success:function(data){
            FindCommentSon(i,T);
        }
    });
}

function FindCommentSon(i,T){
    let SonCommentContent=document.getElementsByClassName("SonCommentContent")
    $.ajax({
        url:'../findCommentSons',
        type:'post',
        data:{
            commentid:i,
        },
        success:function(data){
            let child=T.lastElementChild;
            while(child){
                T.removeChild(child);
                child=T.lastElementChild;
            }
            // console.log(data)
            if(data.msg=="查询成功"){
                // console.log(data)
                for(let a=0;a<data.data.length;a++){
                    if(sessionStorage.getItem("id")==data.data[a].userid){
                        T.innerHTML+='<div class="SonContentS">'+
                            '<div class="deleteSon">删除</div>'+'<div class="replySon">回复</div>'+'<span class="bloggerIdC">'+data.data[a].bloggerid+'</span>'+
                            '<span class="COLOR">'+data.data[a].username+'</span>'+'<span class="reply">回复</span>'+'<span class="COLOR">'+data.data[a].bloggername+'：'+'</span>'+data.data[a].content+
                            '<div class="commentInputCryCCDiv">' +
                            '<textarea class="commentInputCryCC" wrap="virtual" value="发表评论"></textarea>' +
                            '<div class="CommentTrueCC">确定发布</div>' +
                            '<div class="CommentFalseCC">取消发布</div>' +
                            '</div>'+
                            '<span class="commentIdC">'+data.data[a].commentid+'</span>'+
                            '<span class="commentsonid">'+data.data[a].commentsonid+'</span>'+
                            '</div>'
                    }else{
                        T.innerHTML+='<div class="SonContentS">'+
                            '<div class="deleteSon" style="display: none">删除</div>'+'<div class="replySon">回复</div>'+'<span class="bloggerIdC">'+data.data[a].bloggerid+'</span>'+
                            '<span class="COLOR">'+data.data[a].username+'</span>'+'<span class="reply">回复</span>'+'<span class="COLOR">'+data.data[a].bloggername+'：'+'</span>'+data.data[a].content+
                            '<div class="commentInputCryCCDiv">' +
                            '<textarea class="commentInputCryCC" wrap="virtual" value="发表评论"></textarea>' +
                            '<div class="CommentTrueCC">确定发布</div>' +
                            '<div class="CommentFalseCC">取消发布</div>' +
                            '</div>'+
                            '<span class="commentIdC">'+data.data[a].commentid+'</span>'+
                            '<span class="commentsonid">'+data.data[a].commentsonid+'</span>'+
                            '</div>'
                    }
                    let SonContentS=document.getElementsByClassName("SonContentS");
                    let commentInputCryCCDiv=document.getElementsByClassName("commentInputCryCCDiv");
                    let CommentFalseCC=document.getElementsByClassName("CommentFalseCC");
                    let CommentTrueCC=document.getElementsByClassName("CommentTrueCC");
                    let commentInputCryCC=document.getElementsByClassName("commentInputCryCC");
                    let bloggerIdC=document.getElementsByClassName("bloggerIdC");
                    let commentIdC=document.getElementsByClassName("commentIdC");
                    let AAA=document.getElementsByClassName("AAA");
                    let deleteSon=document.getElementsByClassName("deleteSon");
                    let commentsonid=document.getElementsByClassName("commentsonid");
                    SonContentS[a].bloggerid=data.data[a].bloggerid;
                    SonContentS[a].commentid=data.data[a].commentid;

                    let replySon=document.getElementsByClassName("replySon");
                    for(let i=0;i<replySon.length;i++){
                        replySon[i].onclick=function(){
                            commentInputCryCCDiv[i].style.display="block";
                        }
                        CommentFalseCC[i].onclick=function(){
                            commentInputCryCC[i].value="";
                            commentInputCryCCDiv[i].style.display="none";
                            CommentTrueCC[i].style.backgroundColor="#ccccd7";
                        }
                        commentInputCryCC[i].oninput=function(){
                            if(commentInputCryCC[i].value==""){
                                CommentTrueCC[i].style.backgroundColor="#ccccd7";
                            }else{
                                CommentTrueCC[i].style.backgroundColor="#42bbfd";
                                CommentTrueCC[i].onclick=function(){
                                    SONCOMMENT(bloggerIdC[i].innerHTML,BlogId.innerHTML,commentIdC[i].innerHTML,commentInputCryCC[i].value,sessionStorage.getItem("id"),commentIdC[i].innerHTML,bloggerIdC[i].parentNode.parentNode);
                                    commentInputCryCCDiv[i].style.display="none";
                                    commentInputCryCC[i].value="";
                                }
                            }
                        }
                    }
                    for(let i=0;i<deleteSon.length;i++){
                        deleteSon[i].onclick=function(){
                            let r=confirm("你确定要删除这条评论吗");
                            if (r===true) {
                                DeleteSonComment(commentsonid[i].innerHTML,commentIdC[i].innerHTML,deleteSon[i].parentNode.parentNode);
                            }
                            else {

                            }
                        }
                    }
                }
            }else{

            }
        }
    });
}

function DeleteSonComment(i,a,b){
    $.ajax({
        url:'../deleteCommentSon',
        type:'get',
        data:{
            commentSonid:i
        },
        success:function(data){
            // console.log(data)
            FindCommentSon(a,b);
        }
    });
}
//358 518 572 576 578 588 610
// let headerPictureReadH3=document.getElementsByClassName("headerPictureReadH3")[0];
headerPictureReadH3.onclick=function(){
    location.href=this.getAttribute('href');
}
